package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.pojo.TdMenu;
import com.workManager.pojo.TdRole;
import com.workManager.service.RoleService;
import com.workManager.service.SalaryService;
import com.workManager.vo.ProjectSalaryTempVo;
import com.workManager.vo.ProjectSalaryVo;
import com.workManager.vo.SalarySpreadVo;
import com.workManager.vo.SalaryVo;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;

/**
 * Created by worso on 2017/3/21 0021.
 */
@Controller
@RequestMapping("/salary")
public class SalaryController extends BaseController {

    @Autowired
    private SalaryService salaryService;

    @ApiOperation(value="员工人工成本查询",notes = "员工人工成本查询")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(HttpServletRequest request,
                        @RequestParam(value = "pageNumber",required = false)String pageNumber,
                        @RequestParam(value="pageSize", required = false) String pageSize,
                        @RequestParam(value = "startTime", required =  false) String startTime,
                        @RequestParam(value = "endTime", required =  false) String endTime,
                        @RequestParam(value = "orgName", required = false) String orgName,
                        @RequestParam(value = "userName", required = false) String userName){
        ModelMap modelMap = new ModelMap();

        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        Map<String,Object> queryParam = new HashMap<String, Object>();
        queryParam.put("pagenum",pagenum);
        queryParam.put("pagesize",pagesize);
        queryParam.put("startTime",startTime);
        queryParam.put("endTime",endTime);
        queryParam.put("orgName",orgName);
        queryParam.put("userName",userName);

        PageInfo<SalaryVo> tbSalaryPageInfo = salaryService.queryDetails(queryParam);

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",tbSalaryPageInfo.getTotal());
        dataMap.put("rows",tbSalaryPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value = "员工人工成本分布",notes = "员工人工成本分布")
    @RequestMapping(value = "/staffSalarySpreadInMonth",method = RequestMethod.POST)
    public Object staffSalarySpreadInMonth(
            @RequestParam(value = "month",required = true) String month,
            @RequestParam(value = "userId",required = true) String userId){

        ModelMap modelMap = new ModelMap();
        List<SalarySpreadVo> salarySpreadVoList = salaryService.salarySpreadInMonth(month,null,userId);

        if(salarySpreadVoList.size() == 0){
            String msg = "[" + month + "]月份的人天数据为空！";
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,msg);
        }

        BigDecimal total = new BigDecimal(0);
        for(SalarySpreadVo salarySpreadVo : salarySpreadVoList){
            if(salarySpreadVo.getProjectSalary() ==null){
                salarySpreadVo.setProjectSalary(new BigDecimal(0));
            }else{
                total = total.add(salarySpreadVo.getProjectSalary());
            }
        }

        for(SalarySpreadVo salarySpreadVo : salarySpreadVoList){
            if(total.equals(BigDecimal.ZERO)){
                salarySpreadVo.setRatio(new BigDecimal(0));
            }else {
                salarySpreadVo.setRatio(salarySpreadVo.getProjectSalary().divide(total, 4, RoundingMode.HALF_UP));
            }
        }

        Collections.sort(salarySpreadVoList, new Comparator<SalarySpreadVo>() {
            public int compare(SalarySpreadVo o1, SalarySpreadVo o2) {
                return o2.getProjectSalary().compareTo(o1.getProjectSalary());
            }
        });
        return  setSuccessModelMap(modelMap,salarySpreadVoList);

    }



    @ApiOperation(value ="项目人工成本查询",notes = "项目人工成本查询")
    @RequestMapping(value="/countByProjectInMonth",method = RequestMethod.POST)
    public Object countByProjectInMonth(HttpServletRequest request,
            @RequestParam(value = "pageNumber",required = false) String pageNumber,
            @RequestParam(value="pageSize", required = false) String pageSize,
            @RequestParam(value = "startTime", required =  false) String startTime,
            @RequestParam(value = "endTime", required =  false) String endTime,
            @RequestParam(value = "projectCode", required = false) String projectCode,
            @RequestParam(value = "projectName", required = false) String projectName){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);

        PageInfo<ProjectSalaryVo> projectSalaryVoInfo =  salaryService.countByProjectInMonth(map);

        //分部门统计
        List<ProjectSalaryTempVo> projectSalaryTempVos = salaryService.countByProjectInMonthTemp(map);


        if(projectSalaryVoInfo.getTotal()>0){
            List<ProjectSalaryVo> projectSalaryVoList = projectSalaryVoInfo.getList();

            for(ProjectSalaryVo projectSalaryVo:projectSalaryVoList){
                 for(ProjectSalaryTempVo projectSalaryTempVo:projectSalaryTempVos){

                     if(projectSalaryVo.getProjectId().equals(projectSalaryTempVo.getProjectId())){
                         if(projectSalaryTempVo.getOrganizationId().equals(19)){//售后
                             projectSalaryVo.setAfterSaleProjectSalary(projectSalaryTempVo.getProjectSalary());
                         }
                         if(projectSalaryTempVo.getOrganizationId().equals(129)){//售前
                             projectSalaryVo.setPreSaleProjectSalary(projectSalaryTempVo.getProjectSalary());
                         }
                         if(projectSalaryTempVo.getOrganizationId().equals(130)){//销售
                             projectSalaryVo.setSaleProjectSalary(projectSalaryTempVo.getProjectSalary());
                         }
                         if(projectSalaryTempVo.getOrganizationId().equals(127)){//商务
                             projectSalaryVo.setBusinessSaleProjectSalary(projectSalaryTempVo.getProjectSalary());
                         }

                     }
                 }
            }
        }

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",projectSalaryVoInfo.getTotal());
        dataMap.put("rows",projectSalaryVoInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);

    }

    @ApiOperation(value = "项目人工成本分布",notes = "项目人工成本")
    @RequestMapping(value = "/projectSalarySpreadInMonth",method = RequestMethod.POST)
    public Object projectSalarySpreadInMonth(
            @RequestParam(value = "month",required = true) String month,
            @RequestParam(value = "projectId",required = true) String projectId){

        ModelMap modelMap = new ModelMap();
        List<SalarySpreadVo> salarySpreadVoList = salaryService.salarySpreadInMonth(month,projectId,null);

        BigDecimal total = new BigDecimal(0);
        Set<Map<String,Object>> orgNameSet = new HashSet<Map<String,Object>>();

        for(SalarySpreadVo salarySpreadVo : salarySpreadVoList){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id", salarySpreadVo.getOrganizationId());
            map.put("name", salarySpreadVo.getOrgName());
            orgNameSet.add(map);
            if(salarySpreadVo.getProjectSalary() ==null){
                salarySpreadVo.setProjectSalary(new BigDecimal(0));
            }else{
                total = total.add(salarySpreadVo.getProjectSalary());
            }
        }

        for(SalarySpreadVo salarySpreadVo : salarySpreadVoList){
            if(total.equals(BigDecimal.ZERO)){
                salarySpreadVo.setRatio(new BigDecimal(0));
            }else {
                salarySpreadVo.setRatio(salarySpreadVo.getProjectSalary().divide(total, 4, RoundingMode.HALF_UP));
            }
        }

        Collections.sort(salarySpreadVoList, new Comparator<SalarySpreadVo>() {
            public int compare(SalarySpreadVo o1, SalarySpreadVo o2) {
                return o2.getProjectSalary().compareTo(o1.getProjectSalary());
            }
        });

        Map<String,Object> dataMap = new HashMap();
        dataMap.put("orgArray", orgNameSet.toArray());
        dataMap.put("rows", salarySpreadVoList);
        return  setSuccessModelMap(modelMap,dataMap);

    }

    @ApiOperation(value="人工成本Excel文件上传接口",notes="人工成本Excel文件上传接口")
    @RequestMapping(value="/upLoadExcel",method = RequestMethod.POST)
    public Object upLoadExcel(@RequestParam()MultipartFile file){

        if(file==null){
            return null;
        }

        ModelMap modelMap = new ModelMap();
        String name = file.getOriginalFilename();
        long size = file.getSize();

        if(name==null||"".equals(name)&&size==0){
            return null;
        }

        String msg = "上传成功";
        try {
            salaryService.uploadExcel(name,file);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,msg);
        }
        return  setModelMap(modelMap, RespCode.OK,msg);
    }
}
